import { roleApi } from '@/api/index'
import { treeDataTranslate } from "@/util/index";
const role = {
    namespaced: true,
    state: {
        roles: {
            list: [],
            pageNum: 1,
            total: 0
        },
        role: {
            resourceIds: [],
            id: 0,
            name: '',
            status: 1,
            sort: 0
        }
    },
    getters: {
        roleList(state) {
            return state.roles.list
        },
        resourceAll(state, getters, rootState, rootGetters) {
            return treeDataTranslate(rootState.resource.resources.list)
        }
    },
    mutations: {
        SET_ROLES(state, { list, pageNum, total }) {
            state.roles.list = list
            state.roles.pageNum = pageNum
            state.roles.total = total
        },
        SET_ROLE(state, { resourceIds,id, name, status, sort }) {
            state.role.resourceIds=resourceIds
            state.role.id = id
            state.role.name = name
            state.role.status = status
            state.role.sort = sort
        }
    },
    actions: {
        GET_ROLES({ commit }, param) {
            return new Promise((resolve, reject) => {
                roleApi.selectList(param).then(response => {
                    const { code, message, data } = response.data
                    if (code === 200) {
                        commit('SET_ROLES', data)
                        resolve(message)
                    } else {
                        commit('SET_ROLES', [])
                        reject(message)
                    }
                }).catch(error => {
                    reject(error)
                })
            })

        },

        GET_ROLE_BY_ID({ commit }, id) {
            return new Promise((resolve, reject) => {
                let role = {
                    resourceIds: [],
                    id: 0,
                    name: '',
                    status: 1,
                    sort: 0
                }
                roleApi.selectById(id).then(response => {
                    const { code, message, data } = response.data
                    if (code === 200) {
                        role.id = data.id
                        role.name = data.name
                        role.status = data.status
                        role.sort = data.sort
                        roleApi.selectResourcesByRoleId(id).then(response => {
                            const { code, message, data } = response.data
                            if (code === 200) {
                                role.resourceIds = data.map(resource => resource.id)
                                commit('SET_ROLE', role)
                                resolve(message)
                            } else {
                                reject(message)
                            }
                        }).catch(error => {
                            reject(error)
                        })
                    } else {
                        reject(message)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },

        ADD_ROLE({ commit }, param) {
            return new Promise((resolve, reject) => {
                roleApi.add(param).then(response => {
                    const { code, message, data } = response.data
                    if (code === 200) {
                        resolve(message)
                    } else {
                        reject(message)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },
        UPDATE_ROLE({commit},param){
            return new Promise((resolve, reject) => {
                roleApi.update(param).then(response => {
                    const { code, message, data } = response.data
                    if (code === 200) {
                        resolve(message)
                    } else {
                        reject(message)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },
        DELETE_ROLE_BY_IDS({commit},ids){
            return new Promise((resolve, reject) => {
                roleApi.deleteBatch(ids).then(response => {
                    const { code, message, data } = response.data
                    if (code === 200) {
                        resolve(message)
                    } else {
                        reject(message)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        }
    }
}

export default role